﻿using System;
using System.Text.RegularExpressions;

namespace Util.TitleParser
{
    //Format 01x08 and 1x08
    class Parse_sxee : ITitleParser
    {
        public Result ParseTitle(string title)
        {
            string Name, Details;
            long Season, Episode;

            Match m = Regex.Match(title, ".[0-9]x[0-3][0-9]");
            while (m.Success)
            {
                int firstLetterAdjustment;
                if (m.Value[0] != '0' && m.Value[0] != '1' && m.Value[0] != '2')
                {
                    firstLetterAdjustment = 1;
                }
                else if (char.IsDigit(m.Value[0])) //3-9 are impossible
                {
                    m = m.NextMatch();
                    continue;
                }
                else
                {
                    firstLetterAdjustment = 0;
                }

                Season = long.Parse(m.Value.Substring(firstLetterAdjustment, 2 - firstLetterAdjustment), System.Globalization.CultureInfo.InvariantCulture);
                Episode = long.Parse(m.Value.Substring(3, 2), System.Globalization.CultureInfo.InvariantCulture);
                Name = title.Substring(0, m.Index - 1 + firstLetterAdjustment);

                if (m.Index + 5 < title.Length)
                {
                    Details = title.Substring(m.Index + 5);
                }
                else
                {
                    Details = title;
                }
                return new Result(Name, Details, Season, Episode);
            }

            return null;
        }
    }
}
